/**
* @Author  : zou.yingbin
* @Create  : 2022/1/10 11:17
* @Modify  : 2022/1/10 11:17
* @note    :
 */

package rabbitmq

//FieldLogger 定义日志接口
type FieldLogger interface {
	Debug(args ...interface{})
	Info(args ...interface{})
	Warn(args ...interface{})
	Error(args ...interface{})
	Fatal(args ...interface{})
	Panic(args ...interface{})
}

type AckLedger interface {
	Ack(deliveryTag uint64) error
}

// MqConfig 配置
type MqConfig struct {
	ServerName   string // 服务名称
	ExchangeName string // 交换机名称, 如 entry
	ExchangeType string // 交换机类型, 如 topic
	Uri          string // 总线地址, 格式: amqp://guest:guest@192.168.31.249:5673/test
	ServerID     int    // 服务id
}

//MessageCallBack 消息回调
type MessageCallBack func(msg []byte, deliveryTag uint64, ack AckLedger)

//IRabbitMq 接口
type IRabbitMq interface {
	SubscribeTopic(topic string, autoAck, autoDelete bool) //SubscribeTopic 订阅主题, autoDelete为true时,程序退出后会删除订阅的队列
	Publish(topic string, msg []byte) error    //Publish 发送消息
	SetLogger(logger FieldLogger)              //SetLogger 设置日志接口
	Start()                                    //Start 启动
	Stop()                                     //Stop 停止
}

//NewRabbitMq 创建一个rabbitMq对象
func NewRabbitMq(cfg MqConfig, fCall MessageCallBack) IRabbitMq {
	mq := &rabbitMQ{
		cfg: cfg,
		onMsg: fCall,
		logger: &DefaultLog{},
		bRun: true,
	}
	return mq
}

//DefaultMqConfig 缺省配置, ExchangeName=entry, ExchangeType=topic
func DefaultMqConfig(serverName string, serverID int, uri string) MqConfig {
	return MqConfig{
		ServerName: serverName,
		ServerID: serverID,
		Uri: uri,
		ExchangeName: "entry",
		ExchangeType: "topic",
	}
}